﻿using BLL;
using ConvertMediaPro.Models;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ConvertMediaPro.Controllers
{
    public class ReadController : Controller
    {
        //
        // GET: /Read/
        public ActionResult Index(Int64 id)
        {
            ViewData["CategoryId"] = id;
            return View();
        }

        [ValidateInput(false)]
        public ActionResult Save(string content, string title, Int64 categoryId)
        {
            Content_BLL bll = new Content_BLL();
            Int64 returnFilePath = 0;
            if (Request.Files["imgFile"].ContentLength > 0)
            {
                returnFilePath = new UploadController().SaveResource(Request.Files["imgFile"]);
            }
            Model.Content conModel = new Model.Content();
            conModel.Contents = content;
            conModel.CategoryId = categoryId;
            conModel.CreateTime = DateTime.Now;
            conModel.LastUpdateTime = DateTime.Now;
            conModel.Title = title;
            conModel.Cover = returnFilePath.ToString();
            bll.Add(conModel);
            //Int64 contentId = bll.Add2(conModel);

            //ContentViewModel viewModel = new ContentViewModel();
            //viewModel.ContentId=contentId;
            //viewModel.Contents = conModel.Contents;
            //return View(viewModel);
            return RedirectToAction("ContentList", new { id = categoryId });
        }

        public ActionResult Detail(Int64 contentId)
        {

            ContentDetailViewModel model = new ContentDetailViewModel();
            Content_BLL bll = new Content_BLL();
            Model.Content content = bll.GetById(contentId);
            //Content content = bll.GetContent(id);
            model.Content = content;
            Category_BLL catebll = new Category_BLL();
            if (model.Content != null)
            {
                Category cateModel = catebll.GetById(model.Content.CategoryId);
                model.CategoryName = cateModel == null ? "" : cateModel.Name;
            }
            //Content_BLL bll = new Content_BLL();
            //Model.Content model= bll.GetById(contentId);
            return View(model);
        }

        /// <summary>
        /// 又分类进入内容列表
        /// </summary>
        /// <param name="id"><分类ID/param>
        /// <returns></returns>
        public ActionResult ContentList(Int64 id)
        {
            ContentViewModelList model = new ContentViewModelList();

            Content_BLL bll = new Content_BLL();
            Category_BLL catebll = new Category_BLL();
            model.ContentList = bll.SearchContentList(id);
            model.CategoryId = id;
            Category cateModel = catebll.GetById(id);
            model.CategoryName = cateModel==null?"w微信":cateModel.Name;
            if (model.ContentList != null && model.ContentList.Count() > 0)
            {
                model.ContentId = model.ContentList.First().ContentId;
            }
            //model.CategoryId = id;
            return View(model);
        }

        [HttpGet]
        public ActionResult Update(Int64 contentId)
        {
            Content_BLL bll = new Content_BLL();
            Model.Content model = bll.GetById(contentId);
            return View(model);
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Update(ContentViewModel model)
        {
            Content_BLL bll = new Content_BLL();
            Int64 returnFilePath = 0;
            if (Request.Files["imgFile"].ContentLength>0)
            {
                returnFilePath = new UploadController().SaveResource(Request.Files["imgFile"]);
            }
            bool result = bll.Update(model.Contents, model.ContentId, model.Title, returnFilePath.ToString());
            return RedirectToAction("ContentList", new { id=model.CategoryId});
        }

        public ActionResult Delete(Int64 contentId, Int64 categoryId)
        {
            Content_BLL bll = new Content_BLL();
            bool result = bll.Delete(contentId);
            return RedirectToAction("ContentList", new { id = categoryId });
        }

        //public ActionResult MyContent()
        //{
        //    Content_BLL bll = new Content_BLL();
        //    Content content = bll.GetContent();
        //    return View(content);
        //}

        /// <summary>
        /// 阅读各个分类的首条记录
        /// </summary>
        /// <param name="id">分类ID</param>
        /// <returns></returns>
        public ActionResult Content(Int64 id)
        {
            ContentDetailViewModel model = new ContentDetailViewModel();
            Content_BLL bll = new Content_BLL();
            Content content = bll.GetContent(id);
            model.Content = content;
            Category_BLL catebll = new Category_BLL();
            Category cateModel = catebll.GetById(id);
            model.CategoryName = cateModel == null ? "w微信" : cateModel.Name;
            return View("Detail", model);
        }

        public ActionResult SearchListByCategoryId(Int64 id)
        {
            Content_BLL bll = new Content_BLL();
            ContentViewModelList2 model = new ContentViewModelList2();
            model.ContentList = bll.SearchContentList(id);
            return View(model);
        }
	}
}